﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vowei.Core.Models;
using Vowei.Data;
using Vowei.Data.Helpers;
using Vowei.Data.Models;

namespace vowei.test
{
    [TestClass()]
    public class EditingHelpersTest : TestBase
    {
        [TestMethod()]
        public void GenericEndEditNewItemTest()
        {
            var project = new Project()
            {
                Title = "GenericEndEditNewItemTest_Project",
                Owner = Setting.DefaultUserName,
                StartDate = DateTime.Now,
                DueDate = DateTime.Now + TimeSpan.FromDays(30),
                OpenDate = DateTime.Now,
                Reporter = Setting.DefaultUserName,
                ScmType = Project.SupportedScm.Svn,
                ScmUrl = "http://www.vowei.com"
            };
            project.Children.Add(new Milestone()
            {
                Title = "GenericEndEditNewItemTest_Milestone",
                StartDate = DateTime.Now,
                DueDate = DateTime.Now + TimeSpan.FromDays(30),
                OpenDate = DateTime.Now,
                Reporter = Setting.DefaultUserName
            });

            using (var impl = new VoweiContextImpl())
            {
                var context = new VoweiContext(impl);
                EditingHelpers.GenericEndEdit(project, context, Setting.DefaultFormUser);
            }

            using (var context = new VoweiContextImpl())
            {
                var actual = context.Projects.Find(project.ID);
                Assert.IsNotNull(actual);

                Assert.AreEqual<string>(project.Title, actual.Title);
                Assert.AreEqual(project.Owner, actual.Owner);
                Assert.AreEqual(project.StartDate.Value.Year, actual.StartDate.Value.Year);
                Assert.AreEqual(project.StartDate.Value.Month, actual.StartDate.Value.Month);
                Assert.AreEqual(project.StartDate.Value.Day, actual.StartDate.Value.Day);
                Assert.AreEqual(project.StartDate.Value.Hour, actual.StartDate.Value.Hour);
                Assert.AreEqual(project.StartDate.Value.Minute, actual.StartDate.Value.Minute);
                Assert.AreEqual(project.StartDate.Value.Second, actual.StartDate.Value.Second);
                Assert.AreEqual(project.DueDate.Value.Year, actual.DueDate.Value.Year);
                Assert.AreEqual(project.DueDate.Value.Month, actual.DueDate.Value.Month);
                Assert.AreEqual(project.DueDate.Value.Day, actual.DueDate.Value.Day);
                Assert.AreEqual(project.DueDate.Value.Hour, actual.DueDate.Value.Hour);
                Assert.AreEqual(project.DueDate.Value.Minute, actual.DueDate.Value.Minute);
                Assert.AreEqual(project.DueDate.Value.Second, actual.DueDate.Value.Second);
                Assert.AreEqual(project.Reporter, actual.Reporter);
                Assert.AreEqual(project.ScmType, actual.ScmType);
                Assert.AreEqual(project.ScmUrl, actual.ScmUrl);
                Assert.AreEqual(project.Children.Count(), actual.Children.Count());

                Assert.AreEqual(0, project.Children.ElementAt(0).ID.CompareTo(actual.Children.ElementAt(0).ID));
            }
        }

        [TestMethod()]
        public void GenericEndEditModifyItemTest()
        {
            var project = new Project()
            {
                Title = "GenericEndEditModifyItemTest_Project",
                Owner = Setting.DefaultUserName,
                StartDate = DateTime.Now,
                DueDate = DateTime.Now + TimeSpan.FromDays(30),
                OpenDate = DateTime.Now,
                Reporter = Setting.DefaultUserName,
                ScmType = Project.SupportedScm.Svn,
                ScmUrl = "http://www.vowei.com"
            };
            var milestone = new Milestone()
                {
                    Title = "GenericEndEditModifyItemTest_Milestone1",
                    StartDate = DateTime.Now,
                    DueDate = DateTime.Now + TimeSpan.FromDays(30),
                    OpenDate = DateTime.Now,
                    Reporter = Setting.DefaultUserName
                };

            using (var impl = new VoweiContextImpl())
            {
                var context = new VoweiContext(impl);
                project.Children.Add(new Milestone()
                {
                    Title = "GenericEndEditModifyItemTest_Milestone2",
                    StartDate = DateTime.Now,
                    DueDate = DateTime.Now + TimeSpan.FromDays(30),
                    OpenDate = DateTime.Now,
                    Reporter = Setting.DefaultUserName
                });

                impl.Projects.Add(project);
                impl.Milestones.Add(milestone);                
                impl.SaveChanges();

                project.Children.Clear();
                EditingHelpers.GenericEndEdit(project, context, Setting.DefaultFormUser);
            }

            using (var context = new VoweiContextImpl())
            {
                var actual = context.Projects.Find(project.ID);
                Assert.IsNotNull(actual);

                Assert.AreEqual<string>(project.Title, actual.Title);
                Assert.AreEqual(project.Owner, actual.Owner);
                Assert.AreEqual(project.StartDate.Value.Year, actual.StartDate.Value.Year);
                Assert.AreEqual(project.StartDate.Value.Month, actual.StartDate.Value.Month);
                Assert.AreEqual(project.StartDate.Value.Day, actual.StartDate.Value.Day);
                Assert.AreEqual(project.StartDate.Value.Hour, actual.StartDate.Value.Hour);
                Assert.AreEqual(project.StartDate.Value.Minute, actual.StartDate.Value.Minute);
                Assert.AreEqual(project.StartDate.Value.Second, actual.StartDate.Value.Second);
                Assert.AreEqual(project.DueDate.Value.Year, actual.DueDate.Value.Year);
                Assert.AreEqual(project.DueDate.Value.Month, actual.DueDate.Value.Month);
                Assert.AreEqual(project.DueDate.Value.Day, actual.DueDate.Value.Day);
                Assert.AreEqual(project.DueDate.Value.Hour, actual.DueDate.Value.Hour);
                Assert.AreEqual(project.DueDate.Value.Minute, actual.DueDate.Value.Minute);
                Assert.AreEqual(project.DueDate.Value.Second, actual.DueDate.Value.Second);
                Assert.AreEqual(project.Reporter, actual.Reporter);
                Assert.AreEqual(project.ScmType, actual.ScmType);
                Assert.AreEqual(project.ScmUrl, actual.ScmUrl);
                Assert.AreEqual(project.Children.Count(), actual.Children.Count());
            }
        }
    }
}
